/**
 * @(#)WeatherService.java, 2013-2-11. Copyright 2013 RENREN, Inc. All rights
 *                          reserved. RENREN PROPRIETARY/CONFIDENTIAL. Use is
 *                          subject to license terms.
 */
package com.wennimei.service;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wennimei.util.DataUtil;
import com.wennimei.util.FileProcessor;
import com.wennimei.util.FileUtil;
import com.wennimei.util.HttpUtil;

/**
 * @author shichen.feng@renren-inc.com
 */
@Service
public class WeatherService {

    private static final Map<String, String> cityCodeMap = new HashMap<String, String>();

    @PostConstruct
    void init() {
        FileUtil.processFile(DataUtil.WEATHER_CODE, new FileProcessor() {

            @Override
            public void process(String line) {
                String[] array = line.split("=");
                String cityName = array[1];
                String cityCode = array[0];
                cityCodeMap.put(cityName, cityCode);
            }
        });
    }

    @SuppressWarnings("unchecked")
    public String get(String city) {
        try {
            String code = "101010100";
            for (String s: cityCodeMap.keySet()) {
                if (city.contains(s)) {
                    code = cityCodeMap.get(s);
                    break;
                }
            }

            String json = HttpUtil.get(String.format(
                    "http://m.weather.com.cn/data/%s.html", code));
            Gson gson = new Gson();
            Map<String, Object> res = gson.fromJson(json,
                    new TypeToken<Map<String, Object>>() {}.getType());
            Map<String, String> map = (Map<String, String>) res
                    .get("weatherinfo");
            StringBuilder sb = new StringBuilder();
            sb.append(map.get("date_y") + ",")
                    .append(map.get("city") + "\r\n")
                    .append(map.get("temp1") + "\r\n")
                    .append(map.get("weather1") + "\r\n")
                    .append(map.get("wind1") + "\r\n")
                    .append(map.get("index_d") + "\r\n")
                    .append("紫外线[" + map.get("index_uv") + "]\r\n")
                    .append("洗车[" + map.get("index_xc") + "]\r\n")
                    .append("旅游[" + map.get("index_tr") + "]\r\n")
                    .append("舒适指数[" + map.get("index_co") + "]\r\n")
                    .append("晨练[" + map.get("index_cl") + "]\r\n")
                    .append("晾晒[" + map.get("index_ls") + "]\r\n")
                    .append("过敏[" + map.get("index_ag") + "]\r\n")
                    .append("未来两天天气\r\n")
                    .append("明天:" + map.get("weather2") + ","
                            + map.get("temp2") + "\r\n")
                    .append("后天:" + map.get("weather3") + ","
                            + map.get("temp3"));
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
